<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="物料档案编号" prop="materialId">
        <el-input
          v-model="queryParams.materialId"
          placeholder="请输入物料档案编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产批号" prop="batchNo">
        <el-input
          v-model="queryParams.batchNo"
          placeholder="请输入生产批号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库" prop="whId">
        <el-select v-model="queryParams.whId" placeholder="请选择仓库" clearable>
          <el-option
            v-for="dict in sys_com_query"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型：1生产入库2采购入库3领料出库4发货出库" prop="bizType">
        <el-select v-model="queryParams.bizType" placeholder="请选择业务类型：1生产入库2采购入库3领料出库4发货出库" clearable>
          <el-option
            v-for="dict in event_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请日期" style="width: 308px">
        <el-date-picker
          v-model="daterangeInWhDate"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="materialModel">
        <el-input
          v-model="queryParams.materialModel"
          placeholder="请输入型号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格" prop="materialSpecification">
        <el-input
          v-model="queryParams.materialSpecification"
          placeholder="请输入规格"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="materialUnit">
        <el-input
          v-model="queryParams.materialUnit"
          placeholder="请输入单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wmsReport:inWarehouseDetailReport:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wmsReport:inWarehouseDetailReport:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wmsReport:inWarehouseDetailReport:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['wmsReport:inWarehouseDetailReport:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="inWarehouseDetailReportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="生产批号" align="center" prop="batchNo" />
      <el-table-column label="入库数量" align="center" prop="inQuantity" />
      <el-table-column label="业务类型：1生产入库2采购入库3领料出库4发货出库" align="center" prop="bizType">
        <template #default="scope">
          <dict-tag :options="event_type" :value="scope.row.bizType"/>
        </template>
      </el-table-column>
      <el-table-column label="申请日期" align="center" prop="inWhDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.inWhDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="型号" align="center" prop="materialModel" />
      <el-table-column label="规格" align="center" prop="materialSpecification" />
      <el-table-column label="单位" align="center" prop="materialUnit" />
      <el-table-column label="仓库名" align="center" prop="whName" />
      <el-table-column label="库区名称" align="center" prop="whRegionName" />
      <el-table-column label="库位名称" align="center" prop="whLocationName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wmsReport:inWarehouseDetailReport:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wmsReport:inWarehouseDetailReport:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库明细报表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="inWarehouseDetailReportRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料档案编号" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物料档案编号" />
        </el-form-item>
        <el-form-item label="生产批号" prop="batchNo">
          <el-input v-model="form.batchNo" placeholder="请输入生产批号" />
        </el-form-item>
        <el-form-item label="入库数量" prop="inQuantity">
          <el-input v-model="form.inQuantity" placeholder="请输入入库数量" />
        </el-form-item>
        <el-form-item label="仓库" prop="whId">
          <el-select v-model="form.whId" placeholder="请选择仓库">
            <el-option
              v-for="dict in sys_com_query"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型：1生产入库2采购入库3领料出库4发货出库" prop="bizType">
          <el-select v-model="form.bizType" placeholder="请选择业务类型：1生产入库2采购入库3领料出库4发货出库">
            <el-option
              v-for="dict in event_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="申请日期" prop="inWhDate">
          <el-date-picker clearable
            v-model="form.inWhDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择申请日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="型号" prop="materialModel">
          <el-input v-model="form.materialModel" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="规格" prop="materialSpecification">
          <el-input v-model="form.materialSpecification" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="单位" prop="materialUnit">
          <el-input v-model="form.materialUnit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="仓库名" prop="whName">
          <el-input v-model="form.whName" placeholder="请输入仓库名" />
        </el-form-item>
        <el-form-item label="库区名称" prop="whRegionName">
          <el-input v-model="form.whRegionName" placeholder="请输入库区名称" />
        </el-form-item>
        <el-form-item label="库位名称" prop="whLocationName">
          <el-input v-model="form.whLocationName" placeholder="请输入库位名称" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="InWarehouseDetailReport">
import { listInWarehouseDetailReport, getInWarehouseDetailReport, delInWarehouseDetailReport, addInWarehouseDetailReport, updateInWarehouseDetailReport } from "@/api/wmsReport/inWarehouseDetailReport";

const { proxy } = getCurrentInstance();
const { event_type, sys_com_query } = proxy.useDict('event_type', 'sys_com_query');

const inWarehouseDetailReportList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeInWhDate = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    materialId: null,
    batchNo: null,
    whId: null,
    whRegionId: null,
    whLocationId: null,
    bizType: null,
    inWhDate: null,
    materialName: null,
    materialModel: null,
    materialSpecification: null,
    materialUnit: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询入库明细报表列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeInWhDate && '' != daterangeInWhDate) {
    queryParams.value.params["beginInWhDate"] = daterangeInWhDate.value[0];
    queryParams.value.params["endInWhDate"] = daterangeInWhDate.value[1];
  }
  listInWarehouseDetailReport(queryParams.value).then(response => {
    inWarehouseDetailReportList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    materialId: null,
    batchNo: null,
    inQuantity: null,
    whId: null,
    whRegionId: null,
    whLocationId: null,
    bizType: null,
    inWhDate: null,
    materialName: null,
    materialModel: null,
    materialSpecification: null,
    materialUnit: null,
    whName: null,
    whRegionName: null,
    whLocationName: null
  };
  proxy.resetForm("inWarehouseDetailReportRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeInWhDate.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.materialId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加入库明细报表";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _materialId = row.materialId || ids.value
  getInWarehouseDetailReport(_materialId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改入库明细报表";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["inWarehouseDetailReportRef"].validate(valid => {
    if (valid) {
      if (form.value.materialId != null) {
        updateInWarehouseDetailReport(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInWarehouseDetailReport(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _materialIds = row.materialId || ids.value;
  proxy.$modal.confirm('是否确认删除入库明细报表编号为"' + _materialIds + '"的数据项？').then(function() {
    return delInWarehouseDetailReport(_materialIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wmsReport/inWarehouseDetailReport/export', {
    ...queryParams.value
  }, `inWarehouseDetailReport_${new Date().getTime()}.xlsx`)
}

getList();
</script>
